

import './BudoDubboHttpApiJavascriptSdk.js'

import { Message } from 'element-ui';
import { MessageBox } from 'element-ui';


// jquery
import $ from 'jquery'
window.$ = $;

// init sdk
var _sdk = new BudoDubboHttpApiJavascriptSdk();
_sdk.setUrl("/api");

_sdk.setResponseHandler(function(_request) {  
    // 有 error
    if( !!_request.error ) {  
      _request.callback.fail(_request.error.error_description, _request);
      return;
    }
  
    var _responseBody = _request.responseBody;
    var _responseEntity =  _request.serialization.deserialize(_responseBody);
  
    // 正常
    if ( "0" == _responseEntity.status + "" ) { // 数字 0 为 false
      _request.callback.success(_responseEntity.result, _request);
      return;
    }
  
    // 未登录
    if( _responseEntity.status == 1103 || _responseEntity.status == 1102 ) { // 未登录
      _responseEntity.result.status = _responseEntity.status;
      _request.callback.fail(_responseEntity.result, _request);
      return;
    }
  
   // 其他错误回调
    _request.callback.fail(_responseEntity.result.error_description, _request);
  
});

_sdk.setCallback({
    success : ( _responseEntity, _request ) => {
      console.log('#147 success', _responseEntity, _request);
    },
  
    fail : ( _error, _request ) => { // 默认错误处理
      // 其他错误默认处理
      console.log('#150 fail, _error=', _error, ', _request=', _request);
  
      if( _error.status == 1103 || _error.status == 1102 ) { // 未登录
        MessageBox.confirm('#175 去登陆?',"需要登录").then(() => { // 跳转前确认
            location.href = '/sso/authorize?55';
          });
  
        return;
      }
  
      Message({
        type : 'success',
        message : '#151' + JSON.stringify(_error) + JSON.stringify(_request.responseBody),
        offset : 150,
        showClose : true
      });
    }
  });
  

window.sdk = _sdk;